//
//  FindViewController.m
//  逗她盒子
//
//  Created by Qiaofeng on 15/6/9.
//  Copyright (c) 2015年 Qiaofeng. All rights reserved.
//

#import "FindViewController.h"
#import "SANetWorkingTask.h"
#import "ListModel.h"
#import "ListTableViewCell.h"
#import "FindDetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface FindViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)ListModel *model;
@end

@implementation FindViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =  [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        }
    return self;
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListModel *model = [[ListModel alloc]init];
    model = [self.array objectAtIndex:indexPath.row];
    static NSString *cellIdentify = @"cell";
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[ListTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
    }
    
    cell.title.text = model.title;
    cell.Description.text = model.Description;
    if (model.imgs.count != 0) {
        NSString *string = [model.imgs objectAtIndex:0];
        [cell.showImage sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:nil];
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailViewController *detail = [[FindDetailViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    ListModel *model = [_array objectAtIndex:indexPath.row];
    detail.url = model.NewUrl;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //网络请求
    [SANetWorkingTask request:@"http://news.maxjia.com/todaynews/?offset=0&limit=30&phone_num=00000000000&pkey=randpkey&os_type=iOS&os_version=8.3&_time=1434335741&version=1.0.0" parmater:nil block:^(NSDictionary *result) {
        NSArray *arr = [result objectForKey:@"result"];
        for (NSDictionary *dic in arr) {
            self.model = [[ListModel alloc]init];
            [_model setValuesForKeysWithDictionary:dic];
            [self.array addObject:_model];
        }
         [self createTableView];
    }];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
